package com.itheima.reggie.controller;

import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.R;
import com.itheima.reggie.entity.dto.DishDto;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired(required = false)
    private DishService dishService;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto, HttpSession session) {
        Long empId = (Long) session.getAttribute("employee");
        dishDto.setCreateUser(empId);
        dishDto.setUpdateUser(empId);

        dishService.save(dishDto);

        return R.success("添加菜品成功");
    }

    @GetMapping("/page")
    public R<Page<DishDto>> findByPage(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pageSize, String name) {

        R<Page<DishDto>> result = dishService.findByPage(page, pageSize, name);
        return result;
    }

    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable Long id){
        R<DishDto> result = dishService.findById(id);
        return result;
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto,HttpSession session){
        Long empId = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(empId);

        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>> findByCategoryId(Long categoryId,Integer status){
        R<List<DishDto>> result = dishService.findByCategoryId(categoryId,status);
        return result;
    }

    @DeleteMapping
    public R<String> delete(@RequestParam Long[] ids){
        dishService.delete(ids);
        return R.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@RequestParam List<Long> ids,@PathVariable int status){
        dishService.updateStatus(ids,status);

        return R.success("！");
    }
}
